<?php

// ------------------------------------------------------------------------
// |@Author       : Jarmin <topextend@qq.com>
// |@----------------------------------------------------------------------
// |@Date         : 2025-05-11 01:44:49
// |@----------------------------------------------------------------------
// |@LastEditTime : 2025-05-11 02:26:27
// |@----------------------------------------------------------------------
// |@LastEditors  : Jarmin <topextend@qq.com>
// |@----------------------------------------------------------------------
// |@Description  : 接口版本仓库
// |@----------------------------------------------------------------------
// |@FilePath     : ApiVersionRepository.php
// |@----------------------------------------------------------------------
// |@Copyright (c) 2025 http://www.ladmin.cn All Rights Reserved.
// ------------------------------------------------------------------------
declare (strict_types=1);

namespace think\admin\repository;

use think\admin\Repository;
use think\admin\exception\DataException;
use think\admin\model\SystemApiVersions;
use think\admin\model\SystemApiInterfaces;
use think\admin\model\SystemApiGrayReleases;

/**
 * 接口版本仓库
 * Class ApiVersionRepository
 * @package app\common\repository
 */
class ApiVersionRepository extends Repository
{
    /**
     * 初始化
     */
    protected function initialize(): void
    {
        // 设置API版本主模型
        $this->model = new SystemApiVersions();
        // 注册API接口模型
        $this->registerModel('SystemApiInterfaces', new SystemApiInterfaces());
        // 注册灰度发布模型
        $this->registerModel('SystemApiGrayReleases', new SystemApiGrayReleases());
    }

    /**
     * 获取接口信息
     * @param string $path
     * @param string $method
     * @return array
     */
    public function getInterfaceInfo(string $path, string $method): ?array
    {
        return $this->remember("api_interface:{$path}_{$method}", function () use ($path, $method) {
            return $this->getModel('SystemApiInterfaces')
                ->where(['path' => $path, 'method' => strtoupper($method)])
                ->findOrEmpty()->toArray();
        });
    }

    /**
     * 获取版本信息
     * @param string $version
     * @return array
     */
    public function getVersionInfo(string $version): ?array
    {
        return $this->remember("api_version:{$version}", function () use ($version) {
            return $this->model->where(['version' => $version])->findOrEmpty()->toArray();
        });
    }

    /**
     * 获取灰度发布信息
     * @param string $version
     * @return array
     */
    public function getGrayReleaseInfo(string $version): ?array
    {
        return $this->remember("api_gray_release:{$version}", function () use ($version) {
            return $this->getModel('SystemApiGrayReleases')
                ->where('version', '>', $version)
                ->where('status', 1)
                ->where('start_time', '<=', date('Y-m-d H:i:s'))
                ->where('end_time', '>=', date('Y-m-d H:i:s'))
                ->order('version', 'desc')
                ->findOrEmpty()->toArray();
        });
    }
}
